/**
 * 登录
 * Created by KY on 2017/4/17.
 */
define(['ldfw/core','art','weui',
        'text!tpl/register.tpl',
        'comps/validate',
        'comps/ajax','comps/session',
        'ldfw/ldlibs/base64','ldfw/ldlibs/md5','ldfw/helper/formatHelper','jquery-weui'],
    function (LDFW,artTemplate,weui,tpl,validate,ajax,session,base64) {
        var RegisterView = LDFW.Container.extend({
            tpl:tpl,
            className:'panel regist-panel',
            events:{
                'change [name="phone_number"]':'checkPhone',
                'click #register':'register',
                'click #sendRegistCode':'sendCode'
            },
            initialize:function(){
                this.intervalKey = 'register-delay-key'
                if(is_custom_skin && is_custom_skin.trim()){
                    this.model.set('is_custom_skin',is_custom_skin)
                    this.model.set('co_no',co_no)
                }
                this.on('before-shown',function(){
                    this.model.set('is_custom_skin',is_custom_skin)
                    this.model.set('co_fixed_telephone',global_config.co_fixed_telephone)
                })
            },
            rendered:function(){
                var registerDelay = session.getItem(this.intervalKey)
                if(registerDelay){
                    this.disableSendCode()
                    this.$el.find('#sendRegistCode').html('重新发送('+registerDelay+')');
                    this.startInterval(registerDelay)
                }
            },

            pickerAddHandler:function(comp){
                const _this = this
                comp.on('change',function(selectedItem){
                    _this.model.set('cust_type',selectedItem.values)
                })
            },
            checkPhone:function(event){
                const self = this
                validate.checkElement($(event.target),'mobile','mobileUnique')
                    .then(function(){
                        $(event.target).removeClass('error')
                        self.enableSendCode()
                    },function(msg){
                        $.alert(msg,'错误')
                        $(event.target).addClass('error')
                        self.disableSendCode()
                    })
            },

            disableSendCode:function(){
                var $sendCodeBtn = this.$el.find('#sendRegistCode')
                $sendCodeBtn.addClass('weui-btn_disabled')
                $sendCodeBtn.prop('disabled',true)
            },
            enableSendCode:function(){
                var $sendCodeBtn = this.$el.find('#sendRegistCode')
                $sendCodeBtn.removeClass('weui-btn_disabled')
                $sendCodeBtn.removeAttr('disabled')
            },
            startInterval:function(delay){
                const _this = this;
                this.disableSendCode()
                let interval = setInterval(function(){
                    if(delay >= 0){
                        session.setItem(_this.intervalKey,delay)
                        _this.$el.find('#sendRegistCode').html('重新发送('+delay+')')
                        delay--
                    }else{
                        _this.removeInterval(interval)
                    }
                },1000)
            },
            removeInterval:function(interval){
                clearInterval(interval)
                session.removeItem(this.intervalKey)
                this.$el.find('#sendRegistCode').html('获取验证码')
                this.enableSendCode();
            },
            register:function(){
                const _this = this
                weui.form.validate('#registerForm',function(error) {
                    if(error){
                        $(error.ele).addClass('error')
                        $(error.ele).one('blur',function(){
                            $(this).removeClass('error')
                        })
                    }else{
                        let phoneNumber = _this.$el.find('[name="phone_number"]').val(),
                            custPassword = _this.$el.find('[name="cust_password"]').val(),
                            validCode = _this.$el.find('[name="valid_code"]').val(),
                            data = {
                                phone_number:phoneNumber,
                                valid_code:validCode,
                                cust_type:_this.model.get('cust_type')||2
                            }
                        data.cust_passwd = hex_md5(phoneNumber+'#'+custPassword);
                        ajax.post('customerRegister.do',data).success(function(resp){
                            if (!resp.resultCode) {
                                $.toptip('恭喜您，注册成功','success')
                                session.flush()
                                LDFW.navigate('authentication')
                            } else {
                                $.alert(resp.resultMsg)
                            }
                        })
                    }
                })
                return false
            },
            sendCode:function(event){
                const _this = this
                let interval = null,
                    phone_number = this.$el.find('[name="phone_number"]').val()
                if(phone_number && phone_number.trim()){
                    ajax.get('sendCusterRegisterCheckCode.do',{phone_no:phone_number})
                        .success(function(resp){
                            if(resp.resultCode){
                                $.alert(resp.resultMsg,function(){
                                    _this.enableSendCode()
                                })
                            }else{
                                _this.startInterval(60)
                            }
                        })
                        .error(function(){
                            _this.removeInterval(interval)
                        })
                }else{
                    $.toptip('请输入手机号码','warning')
                    _this.disableSendCode()
                }
            }
        })
        return new RegisterView()
    }
)
